﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class alter_certificate : BaseForm
    {
        public alter_certificate()
            : base()
        {
        }

        public alter_certificate(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbCertif.DataSource = Utility.certificates(dbContext);
            cbCertif.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE master;");
            sb.AppendLine();

            if (cbCertif.SelectedIndex > 0)
                sb.AppendLine("ALTER CERTIFICATE [" + cbCertif.Text + "]");
            else
                sb.AppendLine("ALTER CERTIFICATE []");

            if (cbRemove.Checked)
            {
                sb.AppendLine("   REMOVE PRIVATE KEY");
            }

            if (cbprivateKey.Checked)
            {
                sb.AppendLine("   WITH PRIVATE KEY FILE = '" + tbPrivatekeyFile.Text + "' ");

                if (tbDecryptPrivateKey.Text != string.Empty)
                {
                    sb.AppendLine("  ,DECRYPTION BY PASSWORD = '" + tbDecryptPrivateKey.Text + "' ");
                }

                if (tbEncryprPrivateKey.Text != string.Empty)
                {
                    sb.AppendLine("  ,ENCRYPTION BY PASSWORD = '" + tbEncryprPrivateKey.Text + "' ");
                }
            }

            if (cbActive.Checked)
            {
                sb.AppendLine("   ACTIVE FOR BEGIN_DIALOG = ON");
            }

            tbCommand.Text = sb.ToString();

        }

        private void EnablePrivateKey()
        {
            lblPathPrivateKey.Enabled = true;
            tbPrivatekeyFile.Enabled = true;
            btnprivateKey.Enabled = true;

            lblDecryptPrivateKey.Enabled = true;
            tbDecryptPrivateKey.Enabled = true;
            lblEcryptPrivateKey.Enabled = true;
            tbEncryprPrivateKey.Enabled = true;
        }

        private void DisableprivateKey()
        {
            lblPathPrivateKey.Enabled = false;
            tbPrivatekeyFile.Enabled = false;
            btnprivateKey.Enabled = false;

            lblDecryptPrivateKey.Enabled = false;
            tbDecryptPrivateKey.Enabled = false;
            lblEcryptPrivateKey.Enabled = false;
            tbEncryprPrivateKey.Enabled = false;
        }

        private void cbCertif_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbRemove_CheckedChanged(object sender, EventArgs e)
        {
            cbprivateKey.Checked = false;
            cbActive.Checked = false;

            Write();
        }

        private void cbprivateKey_CheckedChanged(object sender, EventArgs e)
        {
            if (cbprivateKey.Checked)
            {
                EnablePrivateKey();
            }
            else
            {
                DisableprivateKey();
            }

            cbRemove.Checked = false;
            cbActive.Checked = false;

            Write();
        }

        private void tbPrivatekeyFile_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnprivateKey_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.AddExtension = true;
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbPrivatekeyFile.Text = dialog.FileName;
            }
        }

        private void tbDecryptPrivateKey_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbEncryprPrivateKey_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbActive_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}